package com.fita.entity.order.reverse.cruises;

import com.demo2do.core.entity.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Embeddable;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: 11200
 * @date: 2019/8/21
 */
@Embeddable
public class CruisesRequest {

    /**
     * 备注
     */
    private String remark;

    /**
     * 乘客
     */
    @Type(type = "com.fita.entity.order.reverse.aviation.CommaDelimitedCruisesUserInfoType")
    private List<CruisesUserInfo> cruisesUserInfos;

    /**
     * 添加一个成员信息
     *
     * @param newCruisesUserInfo
     * @return
     */
    public CruisesRequest addCruisesUserInfo(CruisesUserInfo newCruisesUserInfo) {

        if (StringUtils.isBlank(newCruisesUserInfo.getPassportPic())) {
            throw new BusinessException("护照图片为空");
        }
        if (this.cruisesUserInfos == null) {
            this.cruisesUserInfos = new ArrayList<>();
        }
        if (cruisesUserInfos.stream().filter(info -> info.getPassportPic().equals(newCruisesUserInfo.getPassportPic())).findAny().isPresent()) {
            throw new BusinessException("相同的护照图片代码");
        }
        this.cruisesUserInfos.add(newCruisesUserInfo);

        return this;
    }

    /**
     * 添加
     *
     * @param passportPic
     * @return
     */
    public CruisesRequest addCruisesUserInfo(String passportPic) {

        CruisesUserInfo cruisesUserInfo = new CruisesUserInfo(passportPic);

        CruisesRequest cruisesRequest = addCruisesUserInfo(cruisesUserInfo);

        return cruisesRequest;
    }

    /**
     * 删除一个成员信息
     *
     * @param passportPicCode
     * @return
     */
    public CruisesRequest removeCruisesUserInfo(String passportPicCode) {

        if (this.cruisesUserInfos == null) {
            return this;
        }
        List<CruisesUserInfo> collect = cruisesUserInfos.stream().filter(info -> !info.getPassportPic().equals(passportPicCode)).collect(Collectors.toList());

        this.cruisesUserInfos = collect;

        return this;
    }

    /**
     * 修改一个
     *
     * @param cruisesUserInfo
     * @return
     */
    public CruisesRequest editCruisesUserInfo(CruisesUserInfo cruisesUserInfo) {

        if (StringUtils.isBlank(cruisesUserInfo.getPassportPic())) {
            throw new BusinessException("护照图片为空");
        }
        List<CruisesUserInfo> collect = cruisesUserInfos.stream().filter(info -> !info.getPassportPic().equals(cruisesUserInfo.getPassportPic())).collect(Collectors.toList());

        collect.add(cruisesUserInfo);

        this.cruisesUserInfos = collect;

        return this;
    }



    /**
     * 航班用户
     *
     * @description:
     * @author: 11200
     * @date: 2019/8/30
     */
    public static class CruisesUserInfo {

        private String firstName;

        private String lastName;

        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private Date birthday;

        private String passportCode;

        /**
         * 护照到期时间
         */
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private Date passportExpirationTime;

        /**
         * 护照图片
         */
        private String passportPic;

        /**
         * 护照签发国
         */
        private String passportIssuingCountry;

        private String gender;

        /**
         * 国籍
         */
        private String nationality;

        public CruisesUserInfo() {
        }

        /**
         * @param passportPic
         */
        public CruisesUserInfo(String passportPic) {
            this.passportPic = passportPic;
        }

        @Transient
        public boolean isValid() {
            if (passportExpirationTime == null || birthday == null) {
                return false;
            }
            return StringUtils.isNoneBlank(firstName, lastName, passportCode, passportPic, passportIssuingCountry, gender, nationality);
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getPassportCode() {
            return passportCode;
        }

        public void setPassportCode(String passportCode) {
            this.passportCode = passportCode;
        }

        public Date getPassportExpirationTime() {
            return passportExpirationTime;
        }

        public void setPassportExpirationTime(Date passportExpirationTime) {
            this.passportExpirationTime = passportExpirationTime;
        }

        public String getPassportPic() {
            return passportPic;
        }

        public void setPassportPic(String passportPic) {
            this.passportPic = passportPic;
        }

        public String getPassportIssuingCountry() {
            return passportIssuingCountry;
        }

        public void setPassportIssuingCountry(String passportIssuingCountry) {
            this.passportIssuingCountry = passportIssuingCountry;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public String getNationality() {
            return nationality;
        }

        public void setNationality(String nationality) {
            this.nationality = nationality;
        }

        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    }


    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<CruisesUserInfo> getCruisesUserInfos() {
        return cruisesUserInfos;
    }

    public void setCruisesUserInfos(List<CruisesUserInfo> cruisesUserInfos) {
        this.cruisesUserInfos = cruisesUserInfos;
    }
}
